package cn.jsc.controller;

import cn.jsc.dto.sendSmsCodeDto;
import cn.jsc.result.JSONResult;
import cn.jsc.service.IVerifyCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 验证码相关
 * 图片验证码
 */
@RestController
@RequestMapping("/verifycode")
public class VerifycodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService ;

    /**
     * 获取短信验证码
     * @param dto
     * @return
     */
    @PostMapping("/sendSmsCode")
    public JSONResult sendSmsCode(@RequestBody sendSmsCodeDto dto){
        return verifyCodeService.sendSmsCode(dto);
    }

    /**
     * 获取图片验证码
     * @param key
     * @return
     */
    @GetMapping(value = "/imageCode/{key}")
    public JSONResult createImageCode(@PathVariable(value = "key") String key){
        try{
            String baseImageStr = verifyCodeService.createImageCode(key);
            return JSONResult.success(baseImageStr);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error(e.getMessage());
        }
    }
}